package com.ruoyi.scm.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.response.BaseResult;
import com.ruoyi.scm.domain.ScmPurchaseContract;
import com.ruoyi.scm.service.IScmPurchaseContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 采购合同Controller
 *
 * @author Ryan
 * @date 2025-03-12
 */
@Api(tags="供应链SCM-采购管理-采购合同")
@RestController
@RequestMapping("/scm/purchase/contract")
public class ScmPurchaseContractController extends BaseController
{
    @Autowired
    private IScmPurchaseContractService scmPurchaseContractService;

    /**
     * 查询采购合同列表
     */
    @ApiOperation("采购管理-采购合同列表")
    @GetMapping("/list")
    public TableDataInfo<List<ScmPurchaseContract>> getList(ScmPurchaseContract scmPurchaseContract)
    {
        startPage();
        List<ScmPurchaseContract> list = scmPurchaseContractService.getList(scmPurchaseContract);
        return getDataTable(list);
    }

    /**
     * 导出采购合同列表
     */
    @ApiOperation("导出采购管理-采购合同列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScmPurchaseContract scmPurchaseContract)
    {
        List<ScmPurchaseContract> list = scmPurchaseContractService.getList(scmPurchaseContract);
        ExcelUtil<ScmPurchaseContract> util = new ExcelUtil<ScmPurchaseContract>(ScmPurchaseContract.class);
        util.exportExcel(response, list, "采购合同数据");
    }

    /**
     * 获取采购合同详细信息
     */
    @ApiOperation("采购管理-采购合同详细信息")
    @GetMapping(value = "/{id}")
    public BaseResult<ScmPurchaseContract> getById(@PathVariable("id") Long id)
    {
        return BaseResult.success(scmPurchaseContractService.getById(id));
    }

    /**
     * 新增采购合同
     */
    @ApiOperation("新增采购管理-采购合同")
    @PostMapping
    public BaseResult<Integer> add(@RequestBody ScmPurchaseContract scmPurchaseContract)
    {
        // 申请状态 0未提交 1待审核 2已审核
        scmPurchaseContract.setApplicantStatus(0);
        return BaseResult.success(scmPurchaseContractService.add(scmPurchaseContract));
    }

    /**
     * 修改采购合同
     */
    @ApiOperation("修改采购管理-采购合同")
    @PutMapping
    public BaseResult<Integer> edit(@RequestBody ScmPurchaseContract scmPurchaseContract)
    {
        scmPurchaseContract.setApplicantStatus(0);
        scmPurchaseContract.setApprovedId(null);
        scmPurchaseContract.setApprovedStatus(null);
        scmPurchaseContract.setApprovedDate(null);
        scmPurchaseContract.setApprovedComments("");
        return BaseResult.success(scmPurchaseContractService.edit(scmPurchaseContract));
    }
    @ApiOperation("提交采购合同申请")
    @Log(title = "提交采购合同申请", businessType = BusinessType.UPDATE)
    @PutMapping("/submit")
    public BaseResult<Integer> submit(@RequestBody ScmPurchaseContract scmPurchaseContract) {
        scmPurchaseContract.setApplicantStatus(1);
        scmPurchaseContract.setApplicantDate(DateUtils.getNowDate());
        if(scmPurchaseContract.getId() != null) {
            // 修改提交
            ScmPurchaseContract purchaseContract = scmPurchaseContractService.getById(scmPurchaseContract.getId());
            if(purchaseContract == null) {
                return BaseResult.error("此采购合同不存在，不能提交！");
            }
            scmPurchaseContract.setApprovedId(null);
            scmPurchaseContract.setApprovedStatus(null);
            scmPurchaseContract.setApprovedDate(null);
            scmPurchaseContract.setApprovedComments("");
            return BaseResult.success(scmPurchaseContractService.edit(scmPurchaseContract));
        }else {
            // 新增提交
            return BaseResult.success(scmPurchaseContractService.add(scmPurchaseContract));
        }
    }

    @ApiOperation("审核采购合同")
    @Log(title = "审核采购合同", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    public BaseResult<Integer> approve(@RequestBody ScmPurchaseContract scmPurchaseContract) {
        scmPurchaseContract.setApplicantStatus(2);
        scmPurchaseContract.setApprovedDate(DateUtils.getNowDate());
        scmPurchaseContract.setApprovedId(SecurityUtils.getLoginUser().getUser().getUserId());
        return BaseResult.success(scmPurchaseContractService.edit(scmPurchaseContract));
    }

    /**
     * 删除采购合同
     */
    @ApiOperation("删除采购管理-采购合同")
	@DeleteMapping("/{ids}")
    public BaseResult<Integer> remove(@PathVariable Long[] ids)
    {
        return BaseResult.success(scmPurchaseContractService.del(ids));
    }
}
